package service

import (
	"github.com/honwee/awm/internal/model"
	"github.com/honwee/awm/internal/repository"
)

type TraceService struct {
	traceRepo *repository.TraceRepository
}

func NewTraceService(traceRepo *repository.TraceRepository) *TraceService {
	return &TraceService{
		traceRepo: traceRepo,
	}
}

// GetWorkflowTraces 获取工作流的所有追踪日志
func (s *TraceService) GetWorkflowTraces(workflowID string, offset, limit int) ([]model.Trace, int64, error) {
	return s.traceRepo.FindByWorkflowID(workflowID, offset, limit)
}

// GetWorkflowRunTraces 获取工作流运行实例的所有追踪日志
func (s *TraceService) GetWorkflowRunTraces(workflowRunID string, offset, limit int) ([]model.Trace, int64, error) {
	return s.traceRepo.FindByWorkflowRunID(workflowRunID, offset, limit)
}

// GetNodeRunTraces 获取节点运行实例的所有追踪日志
func (s *TraceService) GetNodeRunTraces(nodeRunID string, offset, limit int) ([]model.Trace, int64, error) {
	return s.traceRepo.FindByNodeRunID(nodeRunID, offset, limit)
}

// GetNodeTraces 获取节点的所有追踪日志
func (s *TraceService) GetNodeTraces(nodeID string, offset, limit int) ([]model.Trace, int64, error) {
	return s.traceRepo.FindByNodeID(nodeID, offset, limit)
}

// GetTracesByEvent 根据事件类型和其他条件获取追踪日志
func (s *TraceService) GetTracesByEvent(event, workflowID, workflowRunID, nodeID string, offset, limit int) ([]model.Trace, int64, error) {
	return s.traceRepo.FindByEvent(event, workflowID, workflowRunID, nodeID, offset, limit)
}

// CreateTrace 创建追踪日志
func (s *TraceService) CreateTrace(trace *model.Trace) error {
	return s.traceRepo.Create(trace)
}
